package cn.jwcat.service.impl;

import cn.jwcat.dealEntity.RoomDto;
import cn.jwcat.entity.*;
import cn.jwcat.mapper.RoomMapper;
import cn.jwcat.service.IRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 房屋 服务实现类
 */
@Transactional
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {

	//	SELECT * FROM s_room LEFT JOIN s_user ON s_user.room_id = s_room.room_id LEFT JOIN s_area ON s_area.area_id = s_room.area_id
	//	LEFT JOIN s_staff_area ON  s_staff_area.area_id = s_area.area_id LEFT JOIN s_staff ON s_staff.staff_id = s_staff_area.staff_id
	//	WHERE s_staff.staff_id = 35 ORDER BY s_room.room_id
	public static MPJLambdaWrapper<Room> baseWrapper(Integer staffId) {
		MPJLambdaWrapper<Room> wrapper = new MPJLambdaWrapper<>();
		wrapper
			.selectAll(Room.class)
			.select(User::getUname, User::getAge, User::getTelephone, User::getPicture, User::getPhone, User::getUserRemark)
			.select(Area::getAreaName)
			.leftJoin(User.class, User::getRoomId, Room::getRoomId)
			.leftJoin(Area.class, Area::getAreaId, Room::getAreaId)
			.leftJoin(StaffArea.class, StaffArea::getAreaId, Area::getAreaId)
			.leftJoin(Staff.class, Staff::getStaffId, StaffArea::getStaffId)
			.orderByAsc(Room::getRoomId)
			.eq(Staff::getStaffId, staffId);
		return wrapper;
	}

	@Override
	public List<RoomDto> getList(Integer staffId) {
		MPJLambdaWrapper<Room> wrapper = baseWrapper(staffId);
		return this.getBaseMapper().selectJoinList(RoomDto.class, wrapper);
	}

	@Override
	public List<RoomDto> getListByName(Integer staffId, String name) {
		MPJLambdaWrapper<Room> wrapper = baseWrapper(staffId);
		wrapper.eq(Room::getName, name);
		return this.getBaseMapper().selectJoinList(RoomDto.class, wrapper);
	}
}
